﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Entity;
using HanGu.Common;
namespace HanGu.CRM.Areas.OperationLog.Controllers
{
    public class DefaultController : AdminBaseController
    {
        //
        // GET: /OperationLog/Default/
        private List<OperationLogDTO> GetAll(UniversallySearchParam SearchParma)
        {
            List<OperationLogDTO> oplogs = new List<OperationLogDTO>();
            var sDate = SearchParma.StarDateTime.Value;
            var eDate = SearchParma.EndDateTime.Value;

            while (sDate <= eDate)
            {
                string OplogxmlfilePath = string.Format("{0}\\{1}.log", Server.MapPath("~/OpLog"), sDate.ToString("yyyyMMdd"));
                var tOpLogs = HanGu.Common.XmlUtil.DeserializeForOpLog<List<OperationLogDTO>>(OplogxmlfilePath);
                if (tOpLogs != null)
                {
                    oplogs.AddRange(tOpLogs);
                }
                sDate = sDate.AddDays(1);
            }
            if (!string.IsNullOrEmpty(SearchParma.Ttile))
            {
                oplogs = oplogs.Where(c => c.ModuleName.IndexOf(SearchParma.Ttile) >= 0).ToList();
            }
            if (!string.IsNullOrEmpty(SearchParma.Creator))
            {
                oplogs = oplogs.Where(c => c.Operater.IndexOf(SearchParma.Creator) >= 0).ToList();
            }
            if (!string.IsNullOrEmpty(SearchParma.Lectcher))
            {
                oplogs = oplogs.Where(c => c.Detail.IndexOf(SearchParma.Lectcher) >= 0).ToList();
            }
            var paginatonDTO = SearchParma.Pagination;
            if (paginatonDTO.ReCount)
            {
                paginatonDTO.RowCount = oplogs.Count();
            }
            if (paginatonDTO.IsPaging)
            {
                oplogs = oplogs.OrderByDescending(c => c.OpTime).Skip(paginatonDTO.PageSize * (paginatonDTO.CurrentIndex - 1)).Take(paginatonDTO.PageSize).ToList();
            }
            return oplogs;
        }
        [HttpGet]
        public ActionResult Index()
        {
            int sDay = DateTime.Now.Day - 3;
            sDay = sDay <= 0 ? 1 : sDay;
            int eDay = DateTime.Now.Day - 1;
            eDay = eDay <= 0 ? 1 : eDay;
            SearchParma.StarDateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, sDay);
            SearchParma.EndDateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, eDay);
            ViewData["SearchParma"] = SearchParma;
            var logList = GetAll(SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return View(logList);
        }
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            var logList = GetAll(SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return View(logList);
        }

        [HttpPost]
        public string DeleteAll()
        {
            try
            {
                WriteFile wFile = new WriteFile("/OpLog/", LogType.Daily);
                wFile.DeleteAllFiles();
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";

        }
    }
}
